﻿using System;
using System.Diagnostics;
using System.ServiceProcess;

namespace RemoteWorker.Worker
{
    public partial class RemoteWorkerService : ServiceBase
    {
        private ServerConnectionManager _manager;

        public RemoteWorkerService()
        {
            InitializeComponent();

            if (!EventLog.SourceExists("RemoteWorkerServiceLogSourse"))
                EventLog.CreateEventSource("RemoteWorkerServiceLogSourse", "RemoteWorkerServiceLog");

            eventLog.Source = "RemoteWorkerServiceLogSourse";
            eventLog.Log = "RemoteWorkerServiceLog";
        }

        internal void Start(string[] args)
        {
            OnStart(args);
        }

        internal void ConsoleStop()
        {
            OnStop();
        }

        protected override void OnStart(string[] args)
        {
            Worker worker = new Worker();
            worker.Name = Environment.MachineName;

            _manager = new ServerConnectionManager(worker);
            _manager.Connect();
        }

        protected override void OnStop()
        {
            _manager.Disconnect();
        }
    }
}